home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_11_12 / engbert / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-11  |  8.6 KB  |  354 lines

  1. /*                             MAIN.C
  2. **
  3. **  MAIN module for developing compression algorithms,
  4. **  opens input file and output files.
  5. **
  6. **  Calls the following 'compress library' functions:
  7. **        get_arguments
  8. **        compress
  9. **        decompress
  10. **      input_is_compressed etc.
  11. */
  12.  
  13.  
  14. #include <stdio.h>
  15. #include <string.h>   /* strrchr() */
  16. #include <ctype.h>
  17. #include <signal.h>
  18. #include <sys\types.h>
  19. #include <sys\stat.h>
  20. #include <io.h>
  21. #include <stdlib.h>
  22. #include <dos.h>
  23. #include <fcntl.h>
  24.  
  25. #include "config.h"
  26. #include "proto.h"   /* function prototypes*/
  27.  
  28. #define MAX_LENGTH 128
  29. /*outfile_name, infile_name etc. are all concatenated
  30. from command line, which is not longer than 128 chars */
  31.  
  32. static int exit_stat= 0;
  33. static char outfile_name[MAX_LENGTH] = {""};
  34. static char infile_name[MAX_LENGTH];
  35. static char output_directory[MAX_LENGTH] = {""};
  36.  
  37. void onintr () {
  38.     unlink ( outfile_name );
  39.     exit ( 1 );
  40. }
  41.  
  42.  
  43. writeerr() {
  44.     perror ( outfile_name );
  45.     unlink ( outfile_name );
  46.     exit ( 1 );
  47. }
  48.  
  49.  
  50.  
  51. static struct stat statbuf;
  52.  
  53. #ifdef COMPRESS
  54.  
  55. static long int fsize;  /* uncompressed file size*/
  56.  
  57. /************ copy_mode *********************/
  58.  
  59.  
  60. int copy_mode() {
  61.     int mode;
  62.     if (stat(infile_name, &statbuf)) {
  63.         /* Get stat on input file */
  64.         perror(infile_name);
  65.         exit(0);
  66.     } else {
  67. #ifdef COMPRESS
  68.         fsize = (long) statbuf.st_size;
  69. #endif
  70.         mode = statbuf.st_mode & 07777;
  71.         return mode;
  72.     }
  73. }
  74. #else COMPRESS
  75.  
  76. /*********** set_mode **************************/
  77.  
  78. set_mode(int mode) {
  79.         if (chmod(outfile_name, mode)) {
  80.             perror(outfile_name);
  81.             exit(0);
  82.         } else;
  83. }
  84.  
  85. #endif
  86.  
  87.  
  88. static int date;
  89. static int time;
  90. static union REGS in;
  91. static union REGS out;
  92. static union REGS *inregs = ∈
  93. static union REGS *outregs = &out;
  94.  
  95. #ifdef COMPRESS
  96.  
  97. /********* copy_date_time() *****************/
  98.  
  99. /* copy date and time to compressed file */
  100.  
  101. copy_date_time() {
  102. unsigned int handle;
  103.  
  104.         handle = open(infile_name, O_RDONLY);
  105.         if (!handle) {
  106.             fprintf(stderr, "Can't open file %s\n",
  107.               infile_name);
  108.         } else {
  109.             inregs->x.ax = 0x5700; /* get date,time */
  110.             inregs->x.bx = handle;
  111.             intdos(inregs, outregs);
  112.             if (outregs->x.cflag != 0) {
  113.                 perror(infile_name);
  114.                 exit(0);
  115.             } else {
  116.                 date = outregs->x.cx;
  117.                 time = outregs->x.dx;
  118.             }
  119.         }
  120. }
  121. #else
  122.  
  123. /*************** set_date_time() ********************/
  124.  
  125. set_date_time() {
  126. int handle;
  127.     handle = open (outfile_name, O_WRONLY);
  128.     if (!handle) {
  129.         fprintf(stderr," Can't open file %s\n",
  130.           outfile_name);
  131.         exit(0);
  132.     } else {
  133.         inregs->x.ax = 0x5701;  /* set date and time */
  134.         inregs->x.bx = (int) handle;
  135.         inregs->x.cx = date;
  136.         inregs->x.dx = time;
  137.         intdos(inregs,outregs);
  138.         if (outregs->x.cflag) fprintf(stderr,
  139.          " Can't write date/time to %s\n",
  140.          outfile_name);
  141.         else;
  142.     }
  143. }
  144.  
  145. #endif
  146.  
  147.  
  148. /**********   main ()   ****************************/
  149.  
  150. main(int argc, char **argv ) {
  151.     int overwrite = 0;
  152.     /* Do not overwrite unless given -f flag */
  153.     char **filelist, **fileptr;
  154.     char *cp, *sufp;
  155.     /*cp = char pointer, sufp = suffix pointer */
  156. #ifdef COMPRESS
  157.     FILE *stream = 0;
  158.     char *infile_name_pointer;
  159. #endif
  160.     unsigned int mode;
  161.     int i,j,k;
  162.     char c;
  163.  
  164.     signal ( SIGINT, onintr );
  165.  
  166.     filelist = get_arguments(argc, argv, &overwrite,
  167.      &output_directory[0]);  /* points to filenames*/
  168.     if (*filelist == NULL) {
  169.         /* if no filelist given on command line */
  170.         usage();
  171.     } else {
  172.         /* We can compress any number of files:
  173.            Will only compress regular files
  174.            (no directories)*/
  175.         for (fileptr = filelist; *fileptr; fileptr++) {
  176. #ifdef COMPRESS
  177.             /* Check for 'reserved' extension: */
  178.             if ((cp = strrchr(*fileptr, '.'))
  179.                != NULL ) cp++; /*if '.' present */
  180.             else cp = "   ";
  181.             if ( is_our_extension(cp) ) {
  182.                 /* if they are the same */
  183.                 fprintf(stderr,
  184.                  "%s: already has extension %s, file"
  185.                  "file not changed\n", *fileptr,--cp);
  186.                 continue;
  187.             } else;
  188.             /* Open input file for compression */
  189.             if ((freopen(*fileptr, "rb", stdin))
  190.                    == NULL) {
  191.                 perror(*fileptr);
  192.                 continue;
  193.             } else {
  194.                 strcpy (infile_name,*fileptr);
  195.                 /* set global infile_name*/
  196.                 /* and strip any path: */
  197.                 infile_name_pointer =
  198.                  strrchr(infile_name,'\\');
  199.                 if (!infile_name_pointer)
  200.                  infile_name_pointer =
  201.                   strrchr(infile_name,':');
  202.                 else;
  203.                 if (!infile_name_pointer)
  204.                  infile_name_pointer = infile_name;
  205.                 else infile_name_pointer++;
  206.                 /* point beyond ; or \  */
  207.             }
  208.  
  209.              /* set up output filename,
  210.              = first argument + extension */
  211.  
  212.             if (!stream) {
  213.                 /* Generate output filename */
  214.                 strcpy(outfile_name, output_directory);
  215.                 if (*outfile_name) {
  216.                     c = outfile_name
  217.                      [strlen(outfile_name)-1];
  218.                     if ( c != ':' && c != '\\')
  219.                       strcat(outfile_name,"\\");
  220.                       /* '\' separates directory and file */
  221.                     else;
  222.                 } else;
  223.                 strcat(outfile_name, infile_name_pointer);
  224.                 /* use infile_name_pointer, which has no path */
  225.                 cp = outfile_name;
  226.                 if ((sufp = strrchr(cp, '.')) != NULL )
  227.                    *++sufp = '\0';  /*if there is a '.' */
  228.                 else strcat(outfile_name,".");
  229.                 strcat(outfile_name, return_extension() );
  230.             } else;  /* we have valid output filename */
  231.  
  232. #else COMPRESS
  233.  
  234.             /* Check for 'reserved' extension: */
  235.             cp = *fileptr + strlen(*fileptr) - 3;
  236.             if (is_our_extension(cp));
  237.             /* if file has expected extension, no action */
  238.             else {
  239.                 fprintf(stderr,"%s: extension %s expected "
  240.                 " -- will open file nevertheless\n",
  241.                 *fileptr, return_extension() );
  242.             }
  243.  
  244.             /* Open input file for decompression */
  245.             if ((freopen(*fileptr, "rb", stdin)) == NULL) {
  246.                     perror(*fileptr);
  247.                     continue;
  248.             } else strcpy (infile_name,*fileptr);
  249.             /* set global infile_name*/
  250.             /* Check the magic number */
  251.             if (!input_is_compressed() ){
  252.                 fprintf(stderr,"File %s is not in compressed"
  253.                  " format\n",*fileptr);
  254.                 continue;  /*to to next file*/
  255.             } else;
  256.  
  257. #endif /*COMPRESS*/
  258.  
  259.             /* Check for overwrite of existing file */
  260. #ifdef COMPRESS
  261.             if (!stream)
  262. #endif
  263.             if (overwrite == 0 ) {
  264.                 if (stat(outfile_name, &statbuf) == 0) {
  265.                     char response[2];
  266.                     response[0] = 'n';
  267.                     fprintf(stderr, "%s already exists;",
  268.                       outfile_name);
  269.                     fprintf(stderr," do you wish to over"
  270.                      "write %s (y or n)? ", outfile_name);
  271.                     fflush(stderr);
  272.                     read(2, response, 2);
  273.                     while (response[1] != '\n') {
  274.                        if (read(2, response+1, 1) < 0) {
  275.                           perror("stderr"); break;
  276.                        }
  277.                     }
  278.                     if ( tolower(response[0]) != 'y') {
  279.                         fprintf(stderr, "\tnot overwritten\n");
  280.                         continue;
  281.                     }
  282.                 }
  283.             }
  284. #ifdef COMPRESS
  285.             else;
  286. #endif
  287.  
  288.  
  289. #ifdef COMPRESS
  290. /* if we haven't opened the output file,
  291. ** i.e. if this is our first file */
  292.             if (!stream) {
  293.                 if (  (stream = freopen(outfile_name,
  294.                      "wb", stdout)) == NULL) {
  295.                     perror(outfile_name);
  296.                     exit(0); /* cannot open output file! */
  297.                 } else;
  298.                 write_file_header ();
  299.             } else;
  300.             printf("%-12s",infile_name_pointer);
  301.             /* output input-filename to compressed file */
  302.             fprintf(stderr, "compressing %s:\n", *fileptr);
  303.               /* for multiple files! */
  304.             putw(copy_mode(),stdout);
  305.             /* copy mode byte to file, set fsize */
  306.             copy_date_time();
  307.             putw(date,stdout);
  308.             putw(time,stdout);
  309.             exit_stat = compress(fsize);
  310.             if (exit_stat == WRITE_ERR) writeerr();
  311.             else;
  312.  
  313. #else
  314.             fprintf(stderr,"Uncompressing %s:\n",*fileptr );
  315.             while ( (j = getchar() ) != EOF) {
  316.                 strcpy(outfile_name,output_directory);
  317.                 if (*outfile_name) {
  318.                     c = outfile_name[strlen(outfile_name)-1];
  319.                     if ( c != ':' && c != '\\')
  320.                      strcat(outfile_name,"\\");
  321.                      /* separate directory and file names */
  322.                     else;
  323.                 } else;
  324.                 outfile_name[k = strlen(outfile_name)] = j;
  325.                 for (i = 1; i<12; i++)
  326.                    outfile_name[++k] = getchar();
  327.                 outfile_name[++k] = 0;
  328.                 if (freopen(outfile_name, "wb", stdout)
  329.                     == NULL) {
  330.                     perror(outfile_name);
  331.                     exit(0);
  332.                 } else {
  333.                     fprintf(stderr,"     %s\n", outfile_name);
  334.                     /* get mode, time of compressed file: */
  335.                     mode = getw(stdin);
  336.                     date = getw(stdin);  /* get date */
  337.                     time = getw(stdin);  /* get time */
  338.                     exit_stat = decompress();
  339.                     if (exit_stat == WRITE_ERR) writeerr();
  340.                     else {
  341.                         fclose (stdout);
  342.                         set_date_time();
  343.                         set_mode(mode);
  344.                     }
  345.                 }
  346.             } /* while there are more files to decompress*/
  347. #endif
  348.         }
  349. #ifdef COMPRESS
  350.     fclose (stdout);
  351. #endif
  352.     } /* else */
  353. } /* end of main() */
  354.